package com.ly.mpayment.rest.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.HttpClient;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.Mobile;
import com.ly.mpayment.persist.service.mobile.IMobileService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/mobile/")
public class MobileRest extends AbsRest{
	@Autowired
	IMobileService mobileService;

	@Autowired
	IUserService userService;

	@Path("findAllProvince")
	@GET
	public String findAllProvince(){
		return ResultMessage.getSucceed( JsonUtils.convertListToJson(this.mobileService.findAllProvince(), new String[]{}));
	}

	@SuppressWarnings("static-access")
	@Path("findAllCity")
	@POST
	public String findAllCity(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "province");
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			return ResultMessage.getSucceed( JsonUtils.convertListToJson(this.mobileService.findAllCity(json.getString("province")), new String[]{}));
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("findProvinceCity")
	@POST
	public String findProvinceCity(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "province");
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			return ResultMessage.getSucceed( JsonUtils.convertListToJson(this.mobileService.findProvinceCity(json.getString("province")), new String[]{}));
		}
		return result;
	}

	@Path("findPageMobile")
	@POST
	@SuppressWarnings("static-access")
	public String findPageMobile(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize","phone","province","city","state");
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("phone"))){
				parameters.put(":phone", "%".concat(json.getString("phone").substring(0, 7)));
			}
			if(!StringUtils.isBlank(json.getString("province"))){
				parameters.put(":province", json.getString("province"));
			}
			if(!StringUtils.isBlank(json.getString("city"))){
				parameters.put(":city", json.getString("city"));
			}
			if(!StringUtils.isBlank(json.getString("state"))){
				parameters.put(":state", json.getInt("state"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.mobileService.findPageMobile(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
		}
		return result;
	}

	@Path("updateMobile")
	@POST
	@SuppressWarnings("static-access")
	public String updateMobile(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,  request, userService,"id","city");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			if(json.containsKey("id") && !StringUtils.isBlank(json.getString("id")) && json.getInt("id") > 0 && !StringUtils.isBlank(json.getString("city").trim())){
				Mobile mobile = this.mobileService.findById(json.getLong("id"));
				mobile.setCity(json.getString("city").trim());
				mobile.setProvince(json.getString("province").trim());
				if(mobileService.updateMobile(mobile)){
					return ResultMessage.getSucceed(null);
				}
			}else{
				Mobile mobileS = this.mobileService.findPhone(json.getString("phone"));
				if(mobileS != null){
					return ResultMessage.getFailt("号段已经存在");
				}
				String mobileOld = this.mobileService.findByMaxId();
				Mobile mobile = new Mobile();
				mobile.setCity(json.getString("city").trim());
				mobile.setProvince(json.getString("province").trim());
				mobile.setPhone(json.getString("phone"));
				mobile.setCityCode("888");
				mobile.setPostCode("222");
				mobile.setServiceProvider(json.getString("serviceProvider"));
				mobile.setState(0);
				mobile.setId(Integer.valueOf(mobileOld)+1);
				if(mobileService.createMobile(mobile)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}


	@Path("updateMobileStatus")
	@POST
	@SuppressWarnings("static-access")
	public String updateMobileStatus(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "id","state");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			if(json.getInt("id") > 0 && !StringUtils.isBlank(json.getString("state").trim())){
				Mobile mobile = this.mobileService.findById(json.getLong("id"));
				mobile.setState(json.getInt("state"));
				if(mobileService.updateMobile(mobile)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@Path("fixmobile")
	@GET
	public String fixmobile() {
		List<Mobile> mobiles = mobileService.findAllCityIsnull(true);
		if (!mobiles.isEmpty()) {
			//{"resultcode":"200","reason":"Return Successd!",
			// "result":{"province":"广东","city":"深圳","areacode":"0755","zip":"","company":"移动","card":""}
			// ,"error_code":0}
			for (Mobile mobile : mobiles) {

				net.sf.json.JSONObject city = HttpClient.httpGetJuhe(mobile.getPhone()+ "1111");
				if (city.containsKey("resultcode") && city.getString("resultcode").equals("200")
						&& city.containsKey("result") && !city.getJSONObject("result").isNullObject()) {
					if (city != null && city.getJSONObject("result") != null) {
						// 更新新的电话号段到本地数据库中
						mobile.setCityCode(city.getJSONObject("result").getString("areacode"));
						mobile.setProvince(city.getJSONObject("result").getString("province"));
						mobile.setCity(city.getJSONObject("result").getString("city"));
						mobile.setPostCode(city.getJSONObject("result").getString("zip"));
						mobile.setServiceProvider("中国" + city.getJSONObject("result").getString("company"));
						this.mobileService.updateMobile(mobile);
					}
				}
			}
		}
		mobiles = mobileService.findAllCityIsnull(false);
		if (!mobiles.isEmpty()) {
			for (Mobile mobile : mobiles) {
				net.sf.json.JSONObject city = HttpClient.httpGetJuhe(mobile.getPhone()+ "1111");
				if (city.containsKey("resultcode") && city.getString("resultcode").equals("200")
						&& city.containsKey("result") && !city.getJSONObject("result").isNullObject()) {
					if (city != null && city.getJSONObject("result") != null) {
						// 更新新的电话号段到本地数据库中
						mobile.setCityCode(city.getJSONObject("result").getString("areacode"));
						mobile.setProvince(city.getJSONObject("result").getString("province"));
						mobile.setCity(city.getJSONObject("result").getString("city"));
						mobile.setPostCode(city.getJSONObject("result").getString("zip"));
						mobile.setServiceProvider("中国" + city.getJSONObject("result").getString("company"));
						this.mobileService.updateMobile(mobile);
					}
				}
			}
		}
		return "ok";
	}

}
